package com.hong.singlemode;

/**
 * @Description: 懒汉式-单例
 * @Author: csh
 * @CreateDate: 2018/7/13$ 10:43$
 * @Version: 1.0
 */
public class SingletonLazy {
    /**空构造方法*/
    private SingletonLazy() { }
    /**对象*/
    private static SingletonLazy singletonLazy = null;

    /**提供获取的对象 加同步*/
    public static synchronized SingletonLazy  getSingle(){
        /**如果为空则创建*/
        if(null==singletonLazy){
                singletonLazy = new SingletonLazy();
        }
        return  singletonLazy;
    }


    /**提供获取的对象 双重锁*/
    public static synchronized SingletonLazy  getSingle2(){
        /**如果为空则创建*/
        if(null==singletonLazy){
            synchronized (SingletonLazy.class){
                if(singletonLazy==null){
                    singletonLazy = new SingletonLazy();
                }

            }
        }
        return  singletonLazy;
    }
    /**静态内部类 此方法相对于其他方法更有利于性能方面又可以防止线程安全！*/
    private static class LazyHolder{
        private static final SingletonLazy INSTANCE = new SingletonLazy();
    }

    public static SingletonLazy getSingle3(){
        return LazyHolder.INSTANCE;
    }
}
